package responsechain.handeler;


import log.Log;

import java.util.HashSet;
import java.util.Set;

public abstract class Handler implements HumanCount {
    protected Log log = new Log(getClass());
    private Set<Interview> interviews = new HashSet<>();//已经招聘的人
    private Handler next;

    public boolean next(Interview interview) {
        if (getNext() == null) {
            return true;
        }
        if (getNext().isFull()) {
            log.e(this.getPartName()+": HC满了");
            return getNext().next(interview);
        }
        return getNext().handle(interview);
    }

    public Handler getNext() {
        return next;
    }

    public Handler buildNext(Handler next) {
        this.next = next;
        return next;
    }

    public abstract boolean handle(Interview interview);

    public String chain() {
        return getPartName() + (next != null ? "->" + next.chain() : "");
    }

    public void employee() {
        log.i("--------------" + getPartName() + "---------------");
        getInterView().forEach(log::i);
        log.i("----------------------------------------------");
        if (next != null)
            getNext().employee();
    }

    @Override
    public boolean offer(Interview interview) {
        if (getInterView().size() >= getHc()) {
            return false;
        }
        log.i(interview.getName() + "：被招聘了");
        return getInterView().add(interview);
    }

    @Override
    public Set<Interview> getInterView() {
            return interviews;
    }

    protected abstract String getPartName();
}
